package router

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"zlxBook/controller"
	"zlxBook/middleware"
)

func Run() {
	r := gin.Default()
	//添加路由允许跨域访问
	r.Use(middleware.CORSMiddleware())
	r.Use(middleware.RecoverMiddleWare())
	r.GET("/ping", controller.Ping)
	//写注册功能
	//创建路由组
	IndexGroup := r.Group("v1")
	{
		IndexGroup.GET("/ping", controller.Ping)
		IndexGroup.POST("/api/user/register", controller.Register)
		IndexGroup.POST("/api/user/login", controller.Login)
		IndexGroup.GET("/api/user/info", middleware.Authmiddleware(), controller.Info)
	}
	categoryController := controller.CreateNewCategooryController()
	CateGroup := r.Group("category")
	{
		CateGroup.GET("/get", categoryController.Get)
		CateGroup.GET("/delete/:id", categoryController.Delete)
		CateGroup.POST("/add", categoryController.Create)
		CateGroup.PUT("/update", categoryController.Update)

	}
	postRoutes := r.Group("/post")
	{
		postRoutes.Use(middleware.Authmiddleware())
		postController := controller.NewPostController()
		postRoutes.POST("/add", postController.AddPost)
		postRoutes.GET("/getAll", postController.GetPost)
		postRoutes.PUT("/update", postController.UpdatePost)
		postRoutes.DELETE("/delete", postController.DeletePostByID)
	}
	fmt.Print("项目准备启动")
	r.Run(":8082")
}
